# *_*coding:utf-8 *_*
from concurrent.futures import ThreadPoolExecutor, as_completed

from downloader.single_downloader import SingleDownloader


class ThreadPoolDownload(SingleDownloader):
    def __init__(self, max_workers, *args, **kwargs):
        super(ThreadPoolDownload, self).__init__(*args, **kwargs)
        self.threadpool = ThreadPoolExecutor(max_workers=max_workers)
        self.all_task = []

    def add(self, url, filepath, callback=None, *args, **kwargs):
        self.all_task.append(self.threadpool.submit(self.single_download, [url, filepath, callback]))

    def join(self):
        for future in as_completed(self.all_task):
            data = future.result()
            print("in main: get page {}s success".format(data))
        self.threadpool.shutdown()
